//
//  AddNewFriendWindowController.m
//  HiQ
//
//  Created by shoguncao on 12-8-10.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AddNewFriendWindowController.h"
#import "AddNewFriendCenter.h"
#import "MainWindowController.h"
#import "Buddy.h"
#import "BuddyGroup.h"
#import "BuddyListSource.h"

@implementation AddNewFriendWindowController

@synthesize searchQQNum;
@synthesize buddyPhoto;
@synthesize onAddRequest;
@synthesize buddyName;

- (void)awakeFromNib
{
    buddyInfo = [[Buddy alloc] init];
    
    [buddyPhoto setBordered:NO];
    [buddyPhoto setEnabled:NO];
    [onAddRequest setEnabled:NO];
    [onAddRequest setHidden:YES];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadNewFiendInfo:) name:GetNewFriendInformation object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (IBAction)onSearch:(NSButton *)sender 
{
    NSString *qqNumStr = [searchQQNum stringValue];
    
    Buddy *myselfInfo = [[MainWindowController sharedMainWindowController] myselfInfo];
    if ([qqNumStr intValue] == myselfInfo->buddyQQ) // 添加的是自己
    {
        [self addNewFriendError:NSLocalizedString(@"不能添加自己为好友", nil)];
        return;
    }
    else 
    {
        BuddyListSource *buddyListSource = [[MainWindowController sharedMainWindowController] buddySource];
        NSMutableArray *buddyGroupArray = [buddyListSource buddyGroupArray];
        if ([buddyGroupArray count] > 0)
        {
            BuddyGroup *firstBuddyGroup = [buddyGroupArray objectAtIndex:0];
            for (Buddy *buddy in firstBuddyGroup->buddyList)
            {
                if ([qqNumStr intValue] == buddy->buddyQQ)  // 添加的人已经是自己的好友
                {
                    NSString *infoText = [[NSString alloc] initWithFormat:NSLocalizedString(@"%@ 已经是你的好友", nil), qqNumStr];
                    [self addNewFriendError:infoText];
                    return;
                }
            }
        }
    }
    
    AddNewFriendCenter *addNewFriendCenter = [[MainWindowController sharedMainWindowController] addNewFriendCenter];
    [addNewFriendCenter searchForUser:[qqNumStr intValue]];
}

- (IBAction)onClickAddRequest:(NSButton *)sender
{
    [buddyPhoto setImage:nil];
    [buddyPhoto setBordered:NO];
    [buddyPhoto setEnabled:NO];
    [onAddRequest setEnabled:NO];
    [onAddRequest setHidden:YES];
    [buddyName setStringValue:@""];
    
    NSInteger fromId = [[MainWindowController sharedMainWindowController] myselfInfo].buddyQQ;
    NSInteger toId = buddyInfo.buddyQQ;
    
    Buddy *myselfInfo = [[MainWindowController sharedMainWindowController] myselfInfo];
    if (toId == myselfInfo->buddyQQ) // 添加的是自己
    {
        [self addNewFriendError:NSLocalizedString(@"不能添加自己为好友", nil)];
        return;
    }
    else 
    {
        BuddyListSource *buddyListSource = [[MainWindowController sharedMainWindowController] buddySource];
        NSMutableArray *buddyGroupArray = [buddyListSource buddyGroupArray];
        if ([buddyGroupArray count] > 0)
        {
            BuddyGroup *firstBuddyGroup = [buddyGroupArray objectAtIndex:0];
            for (Buddy *buddy in firstBuddyGroup->buddyList)
            {
                if (toId == buddy->buddyQQ)  // 添加的人已经是自己的好友
                {
                    NSString *infoText = [[NSString alloc] initWithFormat:NSLocalizedString(@"%ld 已经是你的好友", nil), toId];
                    [self addNewFriendError:infoText];
                    return;
                }
            }
        }
    }
    
    AddNewFriendCenter *addNewFriendCenter = [[MainWindowController sharedMainWindowController] addNewFriendCenter];
    [addNewFriendCenter onSendAddNewFriendRequest:fromId to:toId];
    
    NSAlert *alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle:NSLocalizedString(@"确定", nil)];
    [alert setMessageText:NSLocalizedString(@"添加好友请求",nil)];
    NSString *informativeText = [[NSString alloc] initWithFormat:NSLocalizedString(@"已向 %ld 发送好友申请", nil), toId];
    [alert setInformativeText:informativeText];
    [alert beginSheetModalForWindow:[NSApp mainWindow] modalDelegate:nil didEndSelector:nil contextInfo:nil];
}

- (void)loadNewFiendInfo:(NSNotification *)notification
{
    if (isfindBuddy)
    {
        [buddyName setStringValue:buddyInfo->buddyName];
        [buddyPhoto setImage:buddyInfo->onPhoto];
        [onAddRequest setEnabled:YES];
        [onAddRequest setHidden:NO];
    }
    else
    {
        [buddyName setStringValue:NSLocalizedString(@"找不到联系人·", nil)];
        [onAddRequest setHidden:NO];
    }
}

-(void)addNewFriendError:(NSString *)infoText
{
    NSAlert *alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle:NSLocalizedString(@"确定", nil)];
    [alert setMessageText:NSLocalizedString(@"添加好友失败",nil)];
    [alert setInformativeText:infoText];
    [alert setAlertStyle:NSWarningAlertStyle];
    [alert beginSheetModalForWindow:[NSApp mainWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];
}

@end
